﻿using DTM.Model;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;

namespace DTM.Repository.Tests.System
{
    [TestClass()]
    public class PlayerRepositoryTests
    {
        [TestMethod()]
        public void Player_DatabaseCRUD_Test()
        {
            IPlayer player = new Player()
            {
                Name = Utils.RandomString(10),
                Surname = Utils.RandomString(15),
            };
            player.Email = string.Format("{0}.{1}@test.com", player.Name, player.Surname);

            using (var unitOfWork = new UnitOfWork(new DatabaseEntities()))
            {
                var entity = unitOfWork.Players.Insert(player);
                unitOfWork.Commit();

                player = unitOfWork.Players.GetAll().Where(x => x.Id == entity.Id).FirstOrDefault();
                player.Should().NotBeNull();

                entity.Name += "a";
                entity.Surname += "a";
                unitOfWork.Players.Update(entity);

                player = unitOfWork.Players.GetAll().Where(x => x.Id == entity.Id).FirstOrDefault();
                player.Name.Should().Be(entity.Name);
                player.Surname.Should().Be(entity.Surname);

                unitOfWork.Players.Delete(entity.Id);
                unitOfWork.Commit();
                player = unitOfWork.Players.GetAll().Where(x => x.Id == entity.Id).FirstOrDefault();
                player.Should().BeNull();
            }
        }

        [TestMethod()]
        public void Player_Insert_Test()
        {
            var player = new Player()
            {
                Name = Utils.RandomString(10),
                Surname = Utils.RandomString(15),
            };
            player.Email = string.Format("{0}.{1}@test.com", player.Name, player.Surname);

            using (var unitOfWork = new UnitOfWork(new DatabaseEntities()))
            {
                var result = unitOfWork.Players.Insert(player);
                unitOfWork.Commit();
                result.Id.Should().NotBe(0);
            }
        }

        [TestMethod()]
        public void Player_GetAll_Test()
        {
            var player = new Player()
            {
                Name = Utils.RandomString(10),
                Surname = Utils.RandomString(15),
            };
            player.Email = string.Format("{0}.{1}@test.com", player.Name, player.Surname);

            using (var unitOfWork = new UnitOfWork(new DatabaseEntities()))
            {
                unitOfWork.Players.Insert(player);
                unitOfWork.Commit();

                var result = unitOfWork.Players.GetAll().ToList();
                result.Count.Should().BeGreaterThan(0);
            }
        }
    }
}